﻿package frEngine.shader.filters.fragmentFilters
{
    import baseEngine.core.*;
    import com.gengine.resource.*;
    import frEngine.manager.*;
    import frEngine.shader.*;
    import frEngine.shader.filters.*;

    public class TextureFilter extends FragmentFilter
    {
        public var texture:Texture3D;

        public function TextureFilter(param1:Texture3D)
        {
            super([param1]);
            this.texture = param1;
            return;
        }// end function

        override public function dispose() : void
        {
            super.dispose();
            this.texture = null;
            return;
        }// end function

        override public function setRegisterParams(param1:Program3dRegisterInstance) : void
        {
            param1.getParamByName("{texture1}", false).value = this.texture;
            param1.getParamByName("{kilValues}", false).value = killValue;
            param1.getParamByName("{ColorOffset}", false).value = this.colorOffsetValue;
            param1.getParamByName("{colorMultiplier}", false).value = colorMultiplier;
            param1.getParamByName("{uvValue}", false).value = uvValue;
            param1.getParamByName("{UVrotate}", false).value = UVrotate;
            param1.getParamByName("{uvSclaeCenter2}", false).value = uvSclaeCenter2;
            return;
        }// end function

        override public function get programeId() : String
        {
            if (this.texture && (this.texture.loadedAndUpload || this.texture.targetInfo))
            {
                return "textureFilter:" + FilterBase.ATLFormat[this.texture.format] + ",mip:" + this.texture.mipMode;
            }
            return null;
        }// end function

        override public function clone() : FilterBase
        {
            var _loc_1:* = Resource3dManager.instance.getTexture3d(this.texture.request, this.texture.mipMode);
            var _loc_2:* = new TextureFilter(_loc_1);
            return _loc_2;
        }// end function

        override public function createVertexCode(param1:Program3dRegisterInstance, param2:int) : String
        {
            var _loc_3:* = new Array();
            _loc_3.push(new ToBuilderInfo("va0", FilterName.UV, false, 2));
            _loc_3.push(new ToBuilderInfo("vf0", FilterName.V_UvOriginalPos, false, 1));
            var _loc_4:String = "mov           vf0         \t va0        \t\t\t\t\n";
            _loc_4 = param1.toBuild(_loc_4, _loc_3);
            return _loc_4;
        }// end function

        override public function preCreateFragmentUvCoord(param1:Program3dRegisterInstance) : String
        {
            var _loc_2:* = new Array();
            var _loc_3:String = "";
            _loc_2.push(new ToBuilderInfo("ft0", FilterName.F_UVPostion, false, 1));
            var _loc_4:* = FilterName.V_UvOriginalPos;
            _loc_3 = _loc_3 + ("mov          ft0         \t " + _loc_4 + "        \t\t\t\t\t\t\t\n");
            _loc_3 = param1.toBuild(_loc_3, _loc_2);
            return _loc_3;
        }// end function

        override public function createFragmentUvCoord(param1:Program3dRegisterInstance) : String
        {
            var _loc_2:* = new Array();
            var _loc_3:String = "";
            _loc_2.push(new ToBuilderInfo("ft0", FilterName.F_UVPostion, false, 1));
            _loc_2.push(new ToBuilderInfo("fc0", "{uvValue}", false, 1, uvValue));
            _loc_2.push(new ToBuilderInfo("fc2", "{UVrotate}", false, 1, UVrotate));
            _loc_2.push(new ToBuilderInfo("fc3", "{uvSclaeCenter2}", false, 1, uvSclaeCenter2));
            _loc_3 = _loc_3 + "sub          ft0.xy        ft0.xy           \tfc3.zw        \t\n";
            _loc_3 = _loc_3 + "mul          ft0.xy        ft0.xy           \tfc3.xy        \n";
            _loc_3 = _loc_3 + "add          ft0.xy        ft0.xy           \tfc3.zw        \t\n";
            _loc_3 = _loc_3 + "sub\t\t\t\tft0.xy\t\t\t\tft0.xy\t\t\t\tfc2.xy\t\t\t\t\n";
            _loc_3 = _loc_3 + "mov\t\t\t\tft1\t\t\t\t\tfc2\t\t\t\t\t\t\t\t\t\t\n";
            _loc_3 = _loc_3 + "cos\t\t\t\tft0.z\t\t\t\tft1.z\t\t\t\t\t\t\t\t\t\n";
            _loc_3 = _loc_3 + "sin\t\t\t\tft0.w\t\t\t\tft1.z\t\t\t\t\t\t\t\t\t\n";
            _loc_3 = _loc_3 + "mul\t\t\t\tft1.xyzw\t\t\tft0.zwwz\t\t\tft0.xyxy\t\t\t\n";
            _loc_3 = _loc_3 + "sub\t\t\t\tft2.x\t\t\t\tft1.x\t\t\t\tft1.y\t\t\t\t\n";
            _loc_3 = _loc_3 + "add\t\t\t\tft2.y\t\t\t\tft1.z\t\t\t\tft1.w\t\t\t\t\n";
            _loc_3 = _loc_3 + "add\t\t\t\tft0.xy\t\t\t\tft2.xy\t\t\t\tfc2.xy\t\t\t\t\n";
            _loc_3 = _loc_3 + "mul          ft0.xy        ft0.xy           \tfc0.xy        \n";
            _loc_3 = _loc_3 + "add          ft0.xy        ft0.xy      \t\tfc0.zw    \t\t\n";
            _loc_3 = param1.toBuild(_loc_3, _loc_2);
            return _loc_3;
        }// end function

        override public function createFragmentColor(param1:Program3dRegisterInstance, param2:Boolean) : String
        {
            var _loc_3:* = new Array();
            _loc_3.push(new ToBuilderInfo("fs0", "{texture1}", false, 1, this.texture));
            _loc_3.push(new ToBuilderInfo("fc0", "{kilValues}", false, 1, killValue));
            _loc_3.push(new ToBuilderInfo("fc1", "{ColorOffset}", false, 1, colorOffsetValue));
            _loc_3.push(new ToBuilderInfo("fc2", "{colorMultiplier}", false, 1, colorMultiplier));
            var _loc_4:String = "";
            if (!param2)
            {
                _loc_4 = _loc_4 + "sge\t\t\tft0.xy\t   \t\t\t{F_UVPostion}.xy\t\t\t{fcConst1}.x\t\t\t\n";
                _loc_4 = _loc_4 + "sge\t\t\tft1.xy\t   \t\t\t{fcConst1}.z\t\t\t\t{F_UVPostion}.xy\t\t\n";
                _loc_4 = _loc_4 + "mul\t\t\tft0.xy\t   \t\t\tft0.xy\t\t\t\t\t\tft1.xy\t\t\t\t\t\n";
                _loc_4 = _loc_4 + "min\t\t\tft0.x\t   \t\t\tft0.x\t\t\t\t\t\tft0.y\t\t\t\t\t\n";
                _loc_4 = _loc_4 + "sub\t\t\tft0.x\t   \t\t\tft0.x\t\t\t\t\t\t{fcConst1}.z\t\t\t\n";
                _loc_4 = _loc_4 + "kil\t\t\tft0.x\t   \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n";
            }
            _loc_4 = _loc_4 + ("tex        {output}           {F_UVPostion}.xy    " + getSmapleString("fs0", this.texture.format, param2, this.texture.mipMode) + "\t\n");
            if (this.texture.format == TextureFormat.FORMAT_COMPRESSED_ALPHA)
            {
                _loc_4 = _loc_4 + "mul\t\t\t{output}.xyz\t   {output}.xyz\t\t\t{output}.w\t \n";
            }
            _loc_4 = _loc_4 + "sub\t\t\tft0.xyz\t   {output}.xyz\t\tfc0.xyz\t \t\t\t\n";
            _loc_4 = _loc_4 + "dp3\t\t\tft0.x\t   ft0.xyz\t\t\t\t{fcConst1}.zzz \t\n";
            _loc_4 = _loc_4 + "kil\t\t\tft0.x\t   \t\t\t\t\t\t\t\t\t\t \t\n";
            _loc_4 = _loc_4 + "sub       ft0.x    \t{output}.w  \t\tfc0.w\t\t\t\t\n";
            _loc_4 = _loc_4 + "kil       ft0.x    \t\t\t\t\t\t\t\t\t\t\t\n";
            _loc_4 = _loc_4 + "mul         {output}.xyz       {output}.xyz \t\tfc1.w\t\t\n";
            _loc_4 = _loc_4 + "mul         {output}       \t {output}  \t\tfc1\t\t\t\n";
            _loc_4 = _loc_4 + "mul         {output}.xyz       {output}.xyz  \tfc2.w\t\t\n";
            _loc_4 = param1.toBuild(_loc_4, _loc_3);
            return _loc_4;
        }// end function

    }
}
